/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kurvykurvy.image;

import com.kurvykurvy.page.ImageFrame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import javax.swing.JComponent;

/**
 *
 * @author HY
 */
public class ImagePlaceComponent
        extends JComponent
{
    ImageFrame imageFrame;
    LoadedImage loadedImage;
    BufferedImage image;

    double radians = 0;
    Point offset = new Point( 0, 0 );

    Rectangle rotateSpot;
    Rectangle panSpot;
    Rectangle[] resizePickSpots;

    BufferedImage buffer;

    public ImagePlaceComponent( ImageFrame frame )
    {
        imageFrame = frame;
        loadedImage = imageFrame.getLoadedImage();
        image = loadedImage.getImage();
        setBounds( 0, 0, imageFrame.getSize().width, imageFrame.getSize().height );
    }

    private ImagePlaceComponent()
    {
    }

    public void renderOffScreen()
    {
        BufferedImage img = new BufferedImage( getBounds().width, getBounds().height, BufferedImage.TYPE_INT_ARGB );
        Graphics2D g2 = img.createGraphics();

        g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
        g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC );

        AffineTransform originalTransform = g2.getTransform();
        AffineTransform rotatedTransform = (AffineTransform) originalTransform.clone();

        rotatedTransform.translate( ( getBounds().width - image.getWidth() ) / 2, ( getBounds().height - image.getHeight() ) / 2 );
        rotatedTransform.rotate( radians, image.getWidth() / 2, image.getHeight() / 2 );

        g2.setTransform( rotatedTransform );
        if( image != null )
        {
            g2.drawImage( image, null, 0, 0 );
        }
        else
        {
            g2.setColor( Color.red );
            g2.fillRect( 0, 0, 100, 100 );
            g2.setColor( Color.blue );
            g2.fillRect( 0, 0, 10, 10 );
        }
        g2.setTransform( originalTransform );
        g2.dispose();
        buffer = img;
    }

    public void setRotation( double theta )
    {
        this.radians = theta;
        renderOffScreen();
        repaint();
    }

    public void rotate( double theta )
    {
        this.radians += theta;
        renderOffScreen();
        repaint();
    }

    @Override
    protected void paintComponent( Graphics g )
    {
        super.paintComponent( g );
        if( buffer == null )
            renderOffScreen();

        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage( buffer, null, 0, 0 );
        buffer = null;
    }

    public void setImage( LoadedImage loaded )
    {
        this.loadedImage = loaded;
        this.image = loadedImage.getImage();
        this.imageFrame.setLoadedImage( this.loadedImage );
        double max = Math.sqrt( (double) ( image.getWidth() * image.getWidth() ) + (double) ( image.getHeight() * image.getHeight() ) );
        this.setBounds( getLocation().x, getLocation().y, (int) max, (int) max );
        renderOffScreen();
    }
}
